<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>用户列表</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="/themes/admin/css/font.css">
        <link rel="stylesheet" href="/themes/admin/css/xadmin.css">
        <link rel="stylesheet" href="/themes/admin/css/common.css">
        <script src="/themes/admin/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/themes/admin/js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加用户','/admin/user/userAdd',600,400)"><i class="layui-icon"></i>添加用户</button>
                        </div>
                        <div class="layui-card-body layui-table-body layui-table-main">
                            <table class="layui-table layui-form">
                                <thead>
                                  <tr>
                                    <th>
                                        <input type="checkbox" lay-filter="checkall" lay-skin="primary">
                                    </th>
                                      <th>ID</th>
                                      <th>头像</th>
                                      <th>用户名</th>
                                      <th>性别</th>
                                      <th>城市</th>
                                      <th>用户组</th>
                                      <th>最后登录时间</th>
                                      <th>创建时间</th>
                                      <th>状态</th>
                                      <th>操作</th></tr>
                                </thead>
                                <tbody>
                                {volist name="list" id="vo"}
                                <tr>
                                    <td>
                                        <input class="checkUID" type="checkbox" name="id" value="{$vo.uid}"  lay-skin="primary">
                                    </td>
                                    <td>{$vo.uid}</td>
                                    <td><img style="width: 50px" src="{$vo.avatar}"></td>
                                    <td>{$vo.username}</td>
                                    <td>{$vo.gender}</td>
                                    <td>{$vo.city}</td>
                                    <td>{$vo.group.name}</td>
                                    <td>{$vo.update_time|date='Y-m-d'}</td>
                                    <td>{$vo.create_time|date='Y-m-d'}</td>
                                    <td class="td-status">
                                        <input type="checkbox" name="switch" value="{$vo.uid}" lay-filter="switchUserStatus" lay-skin="switch" lay-text="正常|禁用" <?php if($vo['status'] == 0)  { echo 'checked'; } ?> >
                                    <td class="td-manage">
                                    <button class="layui-btn layui-btn-normal" onclick="xadmin.open('编辑','/admin/user/userAdd?uid={$vo.uid}',600,400)">编辑</button>
                                    <button type="button" class="layui-btn layui-btn-danger" onclick="member_del(this,'{$vo.uid}')">删除</button>
                                </td>
                                  </tr>
                                {/volist}
                                </tbody>
                            </table>
                            {$list|raw}
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['form'], function(){
        var  form = layui.form;


        // 监听全选
        form.on('checkbox(checkall)', function(data){

          if(data.elem.checked){
            $('tbody input.checkUID').prop('checked',true);
          }else{
            $('tbody input.checkUID').prop('checked',false);
          }
          form.render('checkbox');
        });

        //监听用户状态开关
          form.on('switch', function(obj){
              if (obj.elem.checked == true){
                  var status = 0;
              }else if (obj.elem.checked == false){
                  var status = 1;
              }
              var uid = obj.elem.attributes.value.value;
              $.ajax({
                  type:"POST",
                  data:{
                      uid:uid,
                      status:status
                  },
                  url:"/admin/user/userStatus",
                  success:function (e) {
                      layer.msg(e.msg)
                  }
              })
              form.render();
          });
      });

       /*用户-停用*/
      function member_stop(obj,id){
          layer.confirm('确认要停用吗？',function(index){

              if($(obj).attr('title')=='启用'){

                //发异步把用户状态进行更改
                $(obj).attr('title','停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!',{icon: 5,time:1000});

              }else{
                $(obj).attr('title','启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!',{icon: 5,time:1000});
              }
              
          });
      }

      /*用户-删除*/
      function member_del(obj,id){
          layer.confirm('确认要删除吗？',function(index){
              $.ajax({
                  type:"POST",
                  data:{
                      UID:id
                  },
                  url:"/admin/user/delUser",
                  success:function (e) {
                      if (e.code == 1){
                          $(obj).parents("tr").remove();
                          layer.msg(e.msg,{icon:1});
                      }else {
                          layer.msg(e.msg, {icon: 2});
                      }
                  }
              });
          });
      }



      function delAll () {
        var ids = [];

        // 获取选中的id 
        $('tbody input.checkUID').each(function(index, el) {
            if($(this).prop('checked')){
               ids.push($(this).val())
            }
        });
  
        layer.confirm('确认删除所选用户？',function(index){
           $.ajax({
               type:"POST",
               data:{
                   UID:ids
               },
               url:"/admin/user/delUser",
               success:function (e) {
                   if (e.code == 1){
                       layer.msg(e.msg, {icon: 1});
                       $(".layui-form-checked").not('.header').parents('tr').remove();
                   }else {
                       layer.msg(e.msg, {icon: 2});
                   }
               }
           });
        });
      }
    </script>
</html>